package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.member.IMemberReservationService;
import com.zzyl.nursing.vo.ReservationCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
@Slf4j
public class MemberReservationController extends BaseController {

    @Autowired
    private IMemberReservationService memberReservationService;

    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.get();
        log.info("userid:{}",userId);
        Integer count = memberReservationService.getCancelledReservationCount(userId);
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List<ReservationCountVo>> countByTime(Long time) {
        List<ReservationCountVo> list = memberReservationService.getCountByTime(time);
        return R.ok(list);
    }

    /**
     * 新增预约
     * @return
     */
    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult save(@RequestBody ReservationDto dto) {
        memberReservationService.saveReservation(dto);
        return success();
    }

    /**
     * 分页查询预约信息
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R<TableDataInfo<Reservation>> page(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                                           Integer status) {
        return R.ok(memberReservationService.getPage(pageNum, pageSize, status));
    }

    /**
     * 取消预约
     * @return
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@PathVariable Long id) {
        memberReservationService.cancel(id);
        return success();
    }
}